﻿using System.Data.SqlClient;
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SoftballStats_BL
{
    public partial class Team
    {
#region Other Properties
        private Guid _ID;
        private string _Name;
        private string _HeadCoach;
        private Guid _SeasonID;

        public Guid ID
        {
            get
            {
                return _ID;
            }
            set
            {
                _ID = value;
            }
        }

        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }

        public string HeadCoach
        {
            get
            {
                return _HeadCoach;
            }
            set
            {
                _HeadCoach = value;
            }
        }

        public Guid SeasonID
        {
            get
            {
                return _SeasonID;
            }
            set
            {
                _SeasonID = value;
            }
        }
#endregion

#region Public Methods
        public static Team GetTeamByName(string teamName)
        {
            Team thisTeam = new Team();
            try
            {
                using (SqlConnection conn = new SqlConnection(Utility.GetConnectiongString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "spGetTeamByName";
                    cmd.Parameters.AddWithValue("@Name", teamName);
                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            thisTeam.ID = Guid.Parse(dr["ID"].ToString());
                            thisTeam.Name = dr["Name"].ToString();
                            thisTeam.HeadCoach = dr["HeadCoach"].ToString();
                            thisTeam.SeasonID = Guid.Parse(dr["SeasonID"].ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Failed to get team", ex);
            }
            return thisTeam;
        }

        public static Team GetTeamByNameAndSeasonID(string teamName, Guid seasonID)
        {
            Team thisTeam = new Team();
            try
            {
                using (SqlConnection conn = new SqlConnection(Utility.GetConnectiongString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "spGetTeamByNameAndSeasonID";
                    if (teamName != "")
                    {
                        cmd.Parameters.AddWithValue("@Name", teamName);
                    }
                    cmd.Parameters.AddWithValue("@SeasonID", seasonID);
                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            thisTeam.ID = Guid.Parse(dr["ID"].ToString());
                            thisTeam.Name = dr["Name"].ToString();
                            thisTeam.HeadCoach = dr["HeadCoach"].ToString();
                            thisTeam.SeasonID = Guid.Parse(dr["SeasonID"].ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Failed to get team", ex);
            }
            return thisTeam;
        }

        public static List<Team> GetTeamsBySeasonID(Guid seasonID)
        {
            List<SoftballStats_BL.Team> lstTeams = new List<SoftballStats_BL.Team>();

            try
            {
                using (SqlConnection conn = new SqlConnection(Utility.GetConnectiongString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "spGetTeamsBySeasonID";
                    cmd.Parameters.AddWithValue("@SeasonID", seasonID);
                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            Team thisTeam = new Team();
                            thisTeam.ID = Guid.Parse(dr["ID"].ToString());
                            thisTeam.Name = dr["Name"].ToString();
                            thisTeam.HeadCoach = dr["HeadCoach"].ToString();
                            lstTeams.Add(thisTeam);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Failed to get Teams", ex);
            }

            return lstTeams;
        }

        public static Team GetTeamByID(Guid teamID)
        {
            Team thisTeam = new Team();
            try
            {
                using (SqlConnection conn = new SqlConnection(Utility.GetConnectiongString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "spGetTeamByID";
                    cmd.Parameters.AddWithValue("@ID", teamID);
                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            thisTeam.ID = Guid.Parse(dr["ID"].ToString());
                            thisTeam.Name = dr["Name"].ToString();
                            thisTeam.HeadCoach = dr["HeadCoach"].ToString();
                            thisTeam.SeasonID = Guid.Parse(dr["SeasonID"].ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Failed to get team", ex);
            }
            return thisTeam;
        }

        public static List<short> GetRecordByTeamID(Guid teamID)
        {
            List<short> lstRecord = new List<short>();

            try
            {
                using (SqlConnection conn = new SqlConnection(Utility.GetConnectiongString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "spGetRecordByTeam";
                    cmd.Parameters.AddWithValue("@TeamID", teamID);
                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            short wins = short.Parse(dr["Wins"].ToString());
                            short losses = short.Parse(dr["Losses"].ToString());
                            lstRecord.Add(wins);
                            lstRecord.Add(losses);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Failed to get season record", ex);
            }

            return lstRecord;
        }
#endregion
    }
}
